package com.aerfafish.spider.neo4j.spider.service.impl;

import com.aerfafish.spider.neo4j.dao.Fund;
import com.aerfafish.spider.neo4j.repository.DateRepository;
import com.aerfafish.spider.neo4j.repository.FundRepository;
import com.aerfafish.spider.neo4j.spider.service.FundService;
import org.neo4j.driver.exceptions.TransientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author aerfafish
 * @date 2021/4/21 3:48 下午
 */
@Service
public class FundServiceImpl implements FundService {

    @Autowired
    FundRepository fundRepository;

    @Autowired
    DateRepository dateRepository;

    @Override
    public void save(Fund fund) throws Exception {
        if (fund.getAssetSize() != null) {
            save(fund.getCode(), fund.getAssetSize());
        }
        save(fund.getCode(), fund.getType());
    }

    @Override
    public void save(String code, String type) throws Exception {
        if (code == null) {
            throw new Exception("fundCode is null");
        }
        if (type == null) {
            type = "";
        }


        try {
            fundRepository.save(code, type);
        } catch (TransientException e) {
            try {
                fundRepository.save(code, type);
            } catch (TransientException e0) {
                e0.printStackTrace();
            }
        }

    }

    @Override
    public void save(String code, Long assetSize) throws Exception {
        if (code == null) {
            throw new Exception("fundCode is null");
        }

        if (assetSize == null) {
            throw new Exception("fundCode: " + code + " assetSize is null");
        }

        try {
            fundRepository.save(code, assetSize);
        } catch (TransientException e) {
            try {
                fundRepository.save(code, assetSize);
            } catch (TransientException e0) {
                e0.printStackTrace();
            }
        }

    }

    @Override
    public void save(String code, String type, Long assetSize, String date) throws Exception {

        if (code == null || date == null) {
            throw new Exception("some param is null");
        }
        if (type == null) {
            type = "";
        }
        try {
            fundRepository.save(code, type, assetSize, date);
        } catch (TransientException | InvalidDataAccessApiUsageException e) {
            try {
                fundRepository.save(code, type, assetSize, date);
            } catch (TransientException | InvalidDataAccessApiUsageException e0) {
                fundRepository.save(code, type, assetSize, date);
            }
        }

    }

    @Override
    public List<String> queryAllFund() {
        List<String> fundCodes = fundRepository.queryAllFund();
        fundCodes.removeIf(Objects::isNull);
        return fundCodes;
    }


}
